package me.numark.view;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import me.numark.R;
import me.numark.fragments.FeedFragment;
import me.numark.model.Feed;
import me.numark.utils.Constants;
import me.numark.utils.Global;
import me.numark.utils.Settings;

/**
 * Created by Evan on 15/7/24.
 */
public class FeedSimpleAdapter extends UltimateViewAdapter<FeedViewHolder> {

    List<Feed> dataList;
    int mFeedType;
    Object mParent;

    public FeedSimpleAdapter(Object parent, List<Feed> dataList, int feedType) {
        this.dataList = dataList;
        mFeedType = feedType;
        mParent = parent;
    }

    @Override
    public FeedViewHolder getViewHolder(View view) {
        return new FeedViewHolder(view, mParent, Constants.FEEDTYPE_NONE,false);
    }

    @Override
    public FeedViewHolder onCreateViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.feed_list_item, parent, false);
        FeedViewHolder vh = new FeedViewHolder(v, mParent, mFeedType,false);
        return vh;
    }

    @Override
    public int getAdapterItemCount() {
        return dataList.size();
    }

    @Override
    public long generateHeaderId(int i) {
        return 0;
    }

    @Override
    public void onBindViewHolder(FeedViewHolder holder, int position) {
        if (position < dataList.size()) {
            Feed feed = dataList.get(position);
            holder.bindData(feed,this,position);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    }

    public void append(Feed item) {
        insert(dataList, item, dataList.size());
    }

    public void append(Feed[] items) {
        int pos = dataList.size();
        for (Feed item : items) {
           // if (!dataList.contains(item))
                dataList.add(item);
        }
        if (dataList.size()>0) {
            long id = dataList.get(0).id;
            if (mFeedType == Constants.FEEDTYPE_TIMELINE) {
                Global.currentMaxMid = id;
                Settings.setFeedMaxMid(id);
            }
        }
        notifyItemRangeInserted(pos, items.length);
    }

    public void remove(int position) {
        remove(dataList, position);
    }

    public void clear() {
        clear(dataList);
    }

    public void removeFromUser(){
       if (mParent instanceof FeedFragment){
           FeedFragment feedFragment = (FeedFragment) this.mParent;
           feedFragment.refresh();
       }
    }
}
